package com.invite.me.wlqq;

import android.os.Environment;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.invite.me.app.MyApplication;

import com.invite.me.beans.bilnddatabean.InterceptBean;
import com.invite.me.util.SDCardUtil;

import java.io.File;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitManager {
    private static final long MAX_CACHE_SIZE = 1024 * 1024 * 10;
    private static RetrofitManager manager;
    private Retrofit mRetrotit;
    public int i;

    private RetrofitManager() {
        init();
    }

    private void init() {
        File file = null;
        if (SDCardUtil.siAvaliable(MAX_CACHE_SIZE)) {
            // storage/sdcard/cache
            file = new File(Environment.getExternalStorageDirectory(), "cache");
        } else {
            file = new File(MyApplication.mCtontext.getCacheDir(), "");

        }
        Cache cache = new Cache(file, MAX_CACHE_SIZE);

        //拦截器

        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {

            //    try {

                    String s = message.replaceAll("%(?![0-9a-fA-F]{2})", "%25").replaceAll("\\+", "%2B");
                    String text = message;
                    if (!TextUtils.isEmpty(text)) {
                        if (text.contains("{") && text.contains("code") && text.contains("msg")) {

                            Gson gson = new Gson();
                            InterceptBean gankInfo = gson.fromJson(text, InterceptBean.class);
//                            if (gankInfo.getCode() == 400) {
//                                if (i == 1) {
//                                    Intent intent = new Intent(MyApplication.getmCurrentActivity(), OverallSituationSpringVoxActivity.class);
//                                    intent.putExtra("name", gankInfo.getMsg());
//                                    MyApplication.getmCurrentActivity().startActivity(intent);
//                                    MyApplication.getmCurrentActivity().overridePendingTransition(R.anim.out_to_shang, R.anim.out_to_down);
//                                    ToastUtils.show(MyApplication.getmCurrentActivity(), gankInfo.getMsg());
//                                    i++;
//                                }
//
//                            } else if (gankInfo.getCode() == 600) {
//                                if (i == 1) {
//                                    Intent intent = new Intent(MyApplication.getmCurrentActivity(), OverallSituationSpringVoxActivity.class);
//                                    intent.putExtra("name", gankInfo.getMsg());
//                                    MyApplication.getmCurrentActivity().startActivity(intent);
//                                    MyApplication.getmCurrentActivity().overridePendingTransition(R.anim.out_to_shang, R.anim.out_to_down);
//                                    ToastUtils.show(MyApplication.getmCurrentActivity(), gankInfo.getMsg());
//                                    i++;
//                                }
//                            }
                        }


                    }


                //      Log.d("TAG", "ApiServiceHelper.createApiService().HttpLoggingInterceptor.log().message -> " + message);
            }
        });
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);


        OkHttpClient.Builder builder = new OkHttpClient.Builder().
                cache(cache).connectTimeout(30, TimeUnit.SECONDS).
                readTimeout(30, TimeUnit.SECONDS).
                writeTimeout(30, TimeUnit.SECONDS).

              /*  cache(cache).connectTimeout(60000, TimeUnit.SECONDS).
                readTimeout(60000, TimeUnit.SECONDS).
                writeTimeout(60000, TimeUnit.SECONDS).*/

                addInterceptor(loggingInterceptor). // 这个会全部打印出来
                //     addInterceptor(new MyIntercepter()). // 这个可以自定义打印内容，但是无法打印服务器返回的内容
                        retryOnConnectionFailure(true);
        mRetrotit = new Retrofit.Builder().baseUrl(Constants.BASE_URL).

                addCallAdapterFactory(RxJava2CallAdapterFactory.create()).
                addConverterFactory(GsonConverterFactory.create()).
                client(builder.build()).build();

    }


    public static synchronized RetrofitManager getManagerInstance() {
        if (manager == null) {
            synchronized (RetrofitManager.class) {
                if (manager == null) {
                    manager = new RetrofitManager();
                }
            }
        }
        return manager;
    }


    public APIServicde
    getApiService() {
        return mRetrotit.create(APIServicde.class);
    }

}
